Online-Academy
Look, Read, Understand, Apply

Java Beans - 2025

JavaBeans are a standard way to create reusable Java components. They are simply Java classes that follow a specific set of rules.

What Is a JavaBean?

A JavaBean is a simple Java class that:

  1. Implements Serializable
  2. Has a public no-argument constructor
  3. Has private fields (data members)
  4. Provides public getters and setters for those fields

A simple Java beans example
Create a class Worker, save it in classes/com/example folder

package com.example;
import java.io.Serializable;
public class Worker implements Serializable {
    private String name;
    private int age;
    // 1. No-arg constructor
    public Worker() {}
    // 2. Getters and Setters
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Using Java Bean
We simply create an object and use getters and setters to store/retrieve data.
public class Main {
    public static void main(String[] args) {
        Worker p = new Worker();     // Create bean object
        // Set values
        p.setName("Dinesh");
        p.setAge(30);
        // Get values
        System.out.println("Name: " + p.getName());
        System.out.println("Age: " + p.getAge());
    }
}
JavaBeans in JSP (Web Example)
  1. First Register Java Beans:

    <jsp:useBean id="person" class="Person" scope="session" />

  2. Set Properties of bean:
    <jsp:setProperty name="person" property="name" value="Dinesh"/>
    <jsp:setProperty name="person" property="age" value="25"/>
    
  3. Get Bean's properties:
    Name: <jsp:getProperty name="person" property="name"/>
    Age: <jsp:getProperty name="person" property="age"/>
    
When to use JavaBeans?
  1. Storing form data in web apps
  2. Passing data between layers (DTOs)
  3. Frameworks like Spring, Hibernate use JavaBean conventions
  4. JSP pages for reading/writing properties

Using JavaBeans inside a web application (WEB folder) with WEB-INF is a very common pattern in JSP/Servlet projects. Below is a clear, step-by-step guide showing exact folder structure, where to put the bean, and how to use it in JSP.

  • Java source files (.java) go inside src.
  • Compiled .class files end up inside WEB-INF/classes.
  • This keeps your JavaBeans protected (not directly accessible from browser), which is the purpose of WEB-INF.
Inside apache Tomcat find webapps folder
myapps (Folder)
beanform.html (file)
beanresult.jsp (file)
WEB-INF (Folder)
web.xml
classes (Folder)
com (Folder)
example (Folder)
Worker.java (file inside example folder)
Worker.class (file inside example folder)

JavaBean Scopes in JSP

In JSP, when you use JavaBeans with <jsp:useBean>, you can place the bean in different scopes. These scopes control how long the bean lives, who can access it, and when it gets destroyed.

ScopeLifetimeAccessible By Where Stored
page For the current JSP page only Only that JSP PageContext
request For a single HTTP request All JSP/Servlets in same request HttpServletRequest
session Until the user session ends Same user across pages HttpSession
application Entire web app lifecycle All users, all pages ServletContext
beanform.html
<form action="beanresult.jsp">
    Name: <input type="text" name="name">
    Age: <input type="text" name="age">
    <input type="submit" value="Submit">
</form>
beanresult.jsp
<jsp:useBean id="p" class="com.example.Worker" scope="session" />
<jsp:setProperty name="p" property="name" param="name"/>
<jsp:setProperty name="p" property="age" param="age"/>

User Info

Name: <jsp:getProperty name="p" property="name"/>
Age: <jsp:getProperty name="p" property="age"/>